package petstore.persistence;

import petstore.persistence.DBUtil;
import petstore.domain.Category;
import petstore.domain.Item;
import petstore.domain.Product;
import petstore.persistence.impl.ItemDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ItemDaoImpl implements ItemDao
{
    public static final String GET_ITEMLIST_BY_PRODUCTID = "SELECT itemId,productId,name,categoryId,quantity,unitPrice,listPrice,description FROM item WHERE productId = ?";
    public static final String GET_ITEM = "SELECT itemId,productId,name,quantity,categoryId,unitPrice,listPrice,description FROM item WHERE itemId = ?";
    @Override
    public void updateInventoryQuantity(Map<String, Object> param)
    {

    }

    @Override
    public int getInventoryQuantity(String itemId) {
        return 0;
    }

    @Override
    public List<Item> getItemListByProductId(String productId) {
        List<Item> itemList = new ArrayList<>();
        try
        {
          //  System.out.println("OKOK");
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(GET_ITEMLIST_BY_PRODUCTID);
            ResultSet resultSet = preparedStatement.executeQuery();
            while(resultSet.next())
            {
                Item item = new Item();
                item.setItemId(resultSet.getString("itemId"));
                item.setProductId(resultSet.getString("productId"));
                item.setCategoryId(resultSet.getString("categoryId"));
                item.setName(resultSet.getString("name"));
                item.setQuantity(resultSet.getInt("quantity"));
                item.setUnitPrice(resultSet.getBigDecimal("unitPrice"));
                item.setListPrice(resultSet.getBigDecimal("listPrice"));

                itemList.add(item);
            }
            DBUtil.closeResultSet(resultSet);
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return itemList;
    }

    @Override
    public Item getItem(String itemId) {

        Item item = null;
        try
        {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(GET_ITEM);
            preparedStatement.setString(1,itemId);

            ResultSet resultSet = preparedStatement.executeQuery();
            if(resultSet.next())
            {
                System.out.println("OKOKOK");
                item = new Item();
                item.setItemId(resultSet.getString("itemId"));
                item.setProductId(resultSet.getString("productId"));
                item.setCategoryId(resultSet.getString("categoryId"));
                item.setName(resultSet.getString("name"));
                item.setQuantity(resultSet.getInt("quantity"));
                item.setUnitPrice(resultSet.getBigDecimal("unitPrice"));
                item.setListPrice(resultSet.getBigDecimal("listPrice"));
                item.setDescription(resultSet.getString("description"));
                System.out.println("a:::" + resultSet.getString("name"));
            }
            DBUtil.closeResultSet(resultSet);
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return item;
    }
}
